package com.upc.cajabancos.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import oracle.jdbc.driver.OracleResultSet;
import oracle.jdbc.driver.OracleTypes;

import com.upc.cajabancos.model.Cliente;
import com.upc.cajabancos.util.DataAccessObjetc;

public class ClienteDAO {
	
	private Connection connection = null;
	
	public ClienteDAO(Connection connection){
		this.connection = connection;
	}
	
	public void prueba() throws Exception {
		
		Connection con = DataAccessObjetc.getConnection();
	    if(con!= null){
	       System.out.println("Got Connection.");
	       DatabaseMetaData meta = con.getMetaData();
	       System.out.println("Driver Name : "+meta.getDriverName());
	       System.out.println("Driver Version : "+meta.getDriverVersion());
	       DataAccessObjetc.close(con, null, null);

	    }else{
		    System.out.println("Could not Get Connection");
	    }
	}
	
	/**
	 * Lista los clientes
	 * @author Cesar Jesus
	 * @since 30/01/2011
	 * @return List<Cliente>
	 */
	public List<Cliente> listar() throws Exception{
		CallableStatement cstmt = null;	
		ResultSet rs = null;
		Cliente cliente = null;
		List<Cliente> listaClientes = new ArrayList<Cliente>();
    	
    	try{
			
			cstmt = connection.prepareCall(DataAccessObjetc.buildProcedureCall("LISTA_CLIENTES",1));
			
			cstmt.registerOutParameter(1, OracleTypes.CURSOR); //REF CURSOR
			cstmt.execute();
			rs = (OracleResultSet)cstmt.getObject(1);
			
			while (rs.next()){
				cliente = new Cliente();
				cliente.setCodigo(rs.getString("co_cliente"));
				cliente.setNombre(rs.getString("no_cliente"));

				listaClientes.add(cliente);
			}
			
    	}
    	catch(Exception e){
    		throw e;
    	}
    	finally {
    		DataAccessObjetc.close(null,cstmt,rs);
		}
    	
    	return listaClientes;
    }

}
